///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2025 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
///////////////////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.checks.annotation;

import static com.google.common.truth.Truth.assertWithMessage;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_INCORRECT_STYLE;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_PARENS_MISSING;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_PARENS_PRESENT;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING;
import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import de.thetaphi.forbiddenapis.SuppressForbidden;

public class AnnotationUseStyleCheckTest extends AbstractModuleTestSupport {

    @Override
    protected String getPackageLocation() {
        return "com/puppycrawl/tools/checkstyle/checks/annotation/annotationusestyle";
    }

    /* Additional test for jacoco, since valueOf()
     * is generated by javac and jacoco reports that
     * valueOf() is uncovered.
     */
    @Test
    public void testElementStyleOptionValueOf() {
        final AnnotationUseStyleCheck.ElementStyleOption option =
            AnnotationUseStyleCheck.ElementStyleOption.valueOf("COMPACT");
        assertWithMessage("Invalid valueOf result")
            .that(option)
            .isEqualTo(AnnotationUseStyleCheck.ElementStyleOption.COMPACT);
    }

    /* Additional test for jacoco, since valueOf()
     * is generated by javac and jacoco reports that
     * valueOf() is uncovered.
     */
    @Test
    public void testTrailingArrayCommaOptionValueOf() {
        final AnnotationUseStyleCheck.TrailingArrayCommaOption option =
            AnnotationUseStyleCheck.TrailingArrayCommaOption.valueOf("ALWAYS");
        assertWithMessage("Invalid valueOf result")
            .that(option)
            .isEqualTo(AnnotationUseStyleCheck.TrailingArrayCommaOption.ALWAYS);
    }

    /* Additional test for jacoco, since valueOf()
     * is generated by javac and jacoco reports that
     * valueOf() is uncovered.
     */
    @Test
    public void testClosingParensOptionValueOf() {
        final AnnotationUseStyleCheck.ClosingParensOption option =
            AnnotationUseStyleCheck.ClosingParensOption.valueOf("ALWAYS");
        assertWithMessage("Invalid valueOf result")
            .that(option)
            .isEqualTo(AnnotationUseStyleCheck.ClosingParensOption.ALWAYS);
    }

    /**
     * Additional test for checking whether the properties in the configuration are set correctly
     * when there is whitespace around them. Not possible to check with the inline config parser
     * as it trims the whitespace around properties while setting them.
     *
     * @throws Exception exception
     */
    @SuppressForbidden
    @Test
    public void testNonTrimmedInput() throws Exception {
        final DefaultConfiguration configuration =
            createModuleConfig(AnnotationUseStyleCheck.class);
        configuration.addProperty("elementStyle", "ignore");
        configuration.addProperty("closingParens", " ignore  ");
        configuration.addProperty("trailingArrayComma", " always");
        final String filePath = getPath("InputAnnotationUseStyleWithTrailingComma.java");
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

        verify(configuration, filePath, expected);
    }

    @Test
    public void testDefault() throws Exception {
        final String[] expected = {
            "13:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "14:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "21:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "23:1: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
            "29:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "30:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "35:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "42:1: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
            "45:5: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
            "53:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "55:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "59:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "88:32: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
            "91:40: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleDifferentStyles.java"), expected);
    }

    /**
     * Test that annotation parens are always present.
     */
    @Test
    public void testParensAlways() throws Exception {
        final String[] expected = {
            "12:1: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
            "27:1: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
            "32:5: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
            "80:32: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
            "83:40: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
            "91:9: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
            "91:31: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleWithParens.java"), expected);
    }

    /**
     * Test that annotation parens are never present.
     */
    @Test
    public void testParensNever() throws Exception {
        final String[] expected = {
            "22:1: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
            "40:1: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
            "43:5: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
            "86:32: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
            "89:40: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleNoParens.java"), expected);
    }

    @Test
    public void testStyleExpanded() throws Exception {
        final String[] expected = {
            "14:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "21:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "29:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "35:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "48:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "50:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "67:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "72:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "80:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
            "84:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleExpanded.java"), expected);
    }

    @Test
    public void testStyleCompact() throws Exception {
        final String[] expected = {
            "52:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
            "56:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
            "76:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
            "83:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
            "88:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleCompact.java"), expected);
    }

    @Test
    public void testStyleCompactNoArray() throws Exception {
        final String[] expected = {
            "13:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "14:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "21:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "29:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "30:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "35:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "52:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "54:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "58:1: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleCompactNoArray.java"), expected);
    }

    @Test
    public void testCommaAlwaysViolations() throws Exception {
        final String[] expected = {
            "12:20: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "15:30: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "20:40: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "24:45: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "28:55: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "36:22: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "36:36: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "38:21: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "38:30: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "41:39: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "41:49: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "44:21: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "44:41: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleNoTrailingComma.java"), expected);
    }

    @Test
    public void testCommaAlwaysViolationsNonCompilable() throws Exception {
        final String[] expected = {
            "15:37: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
            "15:65: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
        };

        verifyWithInlineConfigParser(
                getNonCompilablePath("InputAnnotationUseStyleNoTrailingComma.java"), expected);
    }

    @Test
    public void testCommaAlwaysNoViolations() throws Exception {
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleWithTrailingComma.java"), expected);
    }

    @Test
    public void testCommaAlwaysNoViolationsNonCompilable() throws Exception {
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

        verifyWithInlineConfigParser(
                getNonCompilablePath("InputAnnotationUseStyleWithTrailingComma.java"), expected);
    }

    @Test
    public void testTrailingArrayIgnore() throws Exception {
        final String[] expected = {
            "15:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "23:13: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "35:5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
            "42:9: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleWithTrailingCommaIgnore.java"), expected);
    }

    @Test
    public void testCommaNeverViolations() throws Exception {
        final String[] expected = {
            "16:32: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
            "21:42: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
            "25:46: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
            "29:56: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
            "37:24: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
            "37:39: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
            "43:41: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
            "43:52: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
        };

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleWithTrailingCommaNever.java"), expected);
    }

    @Test
    public void testCommaNeverNoViolations() throws Exception {
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleNoTrailingCommaNever.java"), expected);
    }

    @Test
    public void testEverythingMixed() throws Exception {
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleEverythingMixed.java"), expected);
    }

    @Test
    public void testAnnotationsWithoutDefaultValues() throws Exception {
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyleParams.java"), expected);
    }

    @Test
    public void testGetAcceptableTokens() {
        final AnnotationUseStyleCheck constantNameCheckObj = new AnnotationUseStyleCheck();
        final int[] actual = constantNameCheckObj.getAcceptableTokens();
        final int[] expected = {TokenTypes.ANNOTATION };
        assertWithMessage("Invalid acceptable tokens")
            .that(actual)
            .isEqualTo(expected);
    }

    @Test
    public void testGetOption() {
        final AnnotationUseStyleCheck check = new AnnotationUseStyleCheck();
        try {
            check.setElementStyle("SHOULD_PRODUCE_ERROR");
            assertWithMessage("ConversionException is expected").fail();
        }
        catch (IllegalArgumentException exc) {
            final String messageStart = "unable to parse";

            assertWithMessage("Invalid exception message, should start with: " + messageStart)
                    .that(exc.getMessage())
                    .startsWith(messageStart);
        }
    }

    @Test
    public void testStyleNotInList() throws Exception {
        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

        verifyWithInlineConfigParser(
                getPath("InputAnnotationUseStyle.java"), expected);
    }

}
